// MFCConDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCCon.h"
#include "MFCConDlg.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <malloc.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCConDlg dialog
CString apppath;
CString name("config.ini");

//chuyen char sang cstring
CString Char2CString(char *s, int length)
{
	
	CString d;
	int i;
	for(i=0;i<length;i++)
		d.AppendChar(s[i]);
	d.Trim();
	return d;
	
}

void CMFCConDlg::preload(char *path)
{
	FILE *fg;

	int count,i;
	bool flag = true;
	char temp;
	char buf[11];
	fg = fopen(path,"r");
	if(fg==NULL)
	{
		fclose(fg);
		fg = fopen("config.ini","r");
		if(fg==NULL)
		{
			fclose(fg);
			return;
		}
	}
		
	fscanf(fg,"%d",&m_questsleep);
	fscanf(fg,"%d",&m_sleep);
	fscanf(fg,"%d",&m_lcount);
	fscanf(fg,"%d",&m_ssleep);
	i=-1;
	while(!feof(fg))
	{
		count = -1;
		i = i + 1;
		memset(buf,' ',11);
		do
		{
			flag=true;
			fread(&temp,1,1,fg);
			if(temp == '\n')
			{
				flag = false;
				//break;
			}
			if(flag == true)
			{
				count = count  + 1;
				buf[count] = temp;
			}
			

		}while(flag == true);
		if(count >=1)
		{
			switch(i)
			{
			case 1:
				{
					
					m_chb1 = TRUE;
					m_txtb1 = Char2CString(buf,count+1);
					break;
				}

			case 2:
				{
					m_chb2 = TRUE;
					m_txtb2 = Char2CString(buf,count+1);
					break;
				}
			case 3:
				{
					m_chb3 = TRUE;
					m_txtb3 = Char2CString(buf,count+1);
					break;
				}
			case 4:
				{
					m_chb4 = TRUE;
					m_txtb4 = Char2CString(buf,count+1);
					break;
				}
			case 5:
				{
					m_chb5 = TRUE;
					m_txtb5 = Char2CString(buf,count+1);
					break;
				}
			case 6:
				{
					m_chb6 = TRUE;
					m_txtb6 = Char2CString(buf,count+1);
					break;
				}
			case 7:
				{
					m_chb7 = TRUE;
					m_txtb7 = Char2CString(buf,count+1);
					break;
				}
			case 8:
				{
					m_chb8 = TRUE;
					m_txtb8 = Char2CString(buf,count+1);
					break;
				}
			case 9:
				{
					m_chb9 = TRUE;
					m_txtb9 = Char2CString(buf,count+1);
					break;
				}
			case 10:
				{
					m_chb10 = TRUE;
					m_txtb10 = Char2CString(buf,count+1);
					break;
				}
			default : break;
			}
		}
	}
	
	fclose(fg);
}

//chuyen Cstring sang char
char* ConvertToChar(const CString &s) 
{ 
    int size = s.GetLength(); 
    char *res= new char[size+1]; 
    memset(res,0,size+1); 
    wcstombs(res, s, size+1); 
    return res; 
} 

// gan num byte tu s tai vi tri thu j sang d tai vi tri thu i
void setdata(char *d, char * s,int num ,int i, int j)
{
	int k;
	for(k = 0;k<num;k++)
	{
		d[k+i] = s[k+j];
	}
}
//set num byte d bang ky tu c bat dau tu vi tri thu i
void setcon(char *d, int num, int i, char c)
{
	int k;
	for(k = 0;k<num;k++)
	{
		if((k%3)==2)
			d[k+i] = ' ';
		else
			d[k+i] = c;
	}
}

int TinhByte(bool dk1, bool dk2, bool dk3, bool dk4, bool dk5)
{
	int tong = 0;
	if(dk1==TRUE)
		tong = tong + 12;
	if(dk2==TRUE)
		tong = tong + 12;
	if(dk3==TRUE)
		tong = tong + 12;
	if(dk4==TRUE)
		tong = tong + 12;
	if(dk5==TRUE)
		tong = tong + 12;
	return tong;
}

void setdata(char *d, CString s,int num ,int i)
{
	int k;
	char *temp = ConvertToChar(s);
	for(k = 0;k<num;k++)
	{
		d[k+i] = temp[k];
	}
	free(temp);
}

void setdata(char *d, char *buff, CString s, int i, bool end, int &offset)
{
	char *temp = ConvertToChar(s);
	int index = 0;
	int num = 12;
	int k;
	
	if(strcmp(temp,"ID")==0)
		index = 0;
	else if(strcmp(temp,"CH")==0)
	{
		index = 12;
		offset=i/3;
	}
	else if(strcmp(temp,"DK")==0)
		index = 24;
	else
		index = -1;
	if(end == TRUE)
		num = 11;
	if(index!=-1)
	{
		setdata(d,buff,num,i,index);
	}
	else
	{
		setdata(d,temp,num,i,0);
	}
	
}

CMFCConDlg::CMFCConDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMFCConDlg::IDD, pParent)
	, m_sou(_T(""))
	, m_des(_T(""))
	, m_sleep(0)
	, m_txtb1(_T("00 00 00 00"))
	, m_txtb2(_T("00 00 00 00"))
	, m_txtb3(_T("00 00 00 00"))
	, m_txtb4(_T("00 00 00 00"))
	, m_txtb5(_T("00 00 00 00"))
	, m_chb1(FALSE)
	, m_chb2(FALSE)
	, m_chb3(FALSE)
	, m_chb4(FALSE)
	, m_chb5(FALSE)
	, m_chb6(FALSE)
	, m_chb7(FALSE)
	, m_chb8(FALSE)
	, m_chb9(FALSE)
	, m_chb10(FALSE)
	, m_txtb6(_T("00 00 00 00"))
	, m_txtb7(_T("00 00 00 00"))
	, m_txtb8(_T("00 00 00 00"))
	, m_txtb9(_T("00 00 00 00"))
	, m_txtb10(_T("00 00 00 00"))
	, m_lcount(2)
	, m_questsleep(4000)
	, m_ssleep(1000)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCConDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, txt_sou, m_sou);
	DDX_Text(pDX, txt_des, m_des);
	DDX_Text(pDX, txt_sleep, m_sleep);
	DDX_Text(pDX, IDC_EDIT2, m_txtb1);
	DDX_Text(pDX, IDC_EDIT3, m_txtb2);
	DDX_Text(pDX, IDC_EDIT4, m_txtb3);
	DDX_Text(pDX, IDC_EDIT5, m_txtb4);
	DDX_Text(pDX, IDC_EDIT6, m_txtb5);
	DDX_Check(pDX, ch_b1, m_chb1);
	DDX_Check(pDX, ch_b2, m_chb2);
	DDX_Check(pDX, ch_b3, m_chb3);
	DDX_Check(pDX, ch_b4, m_chb4);
	DDX_Check(pDX, ch_b5, m_chb5);
	DDX_Check(pDX, ch_b6, m_chb6);
	DDX_Check(pDX, ch_b7, m_chb7);
	DDX_Check(pDX, ch_b8, m_chb8);
	DDX_Check(pDX, ch_b9, m_chb9);
	DDX_Check(pDX, ch_b10, m_chb10);
	DDX_Text(pDX, IDC_EDIT7, m_txtb6);
	DDX_Text(pDX, IDC_EDIT8, m_txtb7);
	DDX_Text(pDX, IDC_EDIT9, m_txtb8);
	DDX_Text(pDX, IDC_EDIT10, m_txtb9);
	DDX_Text(pDX, IDC_EDIT11, m_txtb10);
	DDX_Text(pDX, txt_lcount, m_lcount);
	DDV_MaxChars(pDX, m_txtb1, 11);
	DDV_MaxChars(pDX, m_txtb2, 11);
	DDV_MaxChars(pDX, m_txtb3, 11);
	DDV_MaxChars(pDX, m_txtb4, 11);
	DDV_MaxChars(pDX, m_txtb5, 11);
	DDV_MaxChars(pDX, m_txtb10, 11);
	DDV_MaxChars(pDX, m_txtb9, 11);
	DDV_MaxChars(pDX, m_txtb8, 11);
	DDV_MaxChars(pDX, m_txtb7, 11);
	DDV_MaxChars(pDX, m_txtb6, 11);
	DDX_Text(pDX, txt_questsleep, m_questsleep);
	DDX_Text(pDX, txt_ssleep, m_ssleep);
}

BEGIN_MESSAGE_MAP(CMFCConDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(bt_sou, &CMFCConDlg::OnBnClickedsou)
	ON_BN_CLICKED(bt_des, &CMFCConDlg::OnBnClickeddes)
	ON_BN_CLICKED(bt_run, &CMFCConDlg::OnBnClickedrun)
	ON_EN_CHANGE(txt_lcount, &CMFCConDlg::OnEnChangelcount)
END_MESSAGE_MAP()


// CMFCConDlg message handlers



BOOL CMFCConDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	//CString apppath;
	// get App Path
	TCHAR* pstrExePath = apppath.GetBuffer (MAX_PATH);

	::GetModuleFileName (0, pstrExePath, MAX_PATH);
	
	apppath.ReleaseBuffer ();
	
	
	int i;
	int pathlength = apppath.GetLength();
	for(i=pathlength-1;i>=0;i--)
	{
		
		if(apppath[i]=='\\')
		{
			break;
		}
		apppath.Delete(i,1);
			
	}
	apppath = apppath+name;
	if(apppath.IsEmpty()!=TRUE)
	{
		preload(ConvertToChar(apppath));
		UpdateData(FALSE);
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMFCConDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMFCConDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CMFCConDlg::OnBnClickedsou()
{
	// TODO: Add your control notification handler code here
	CFileDialog ma(TRUE);


	if(ma.DoModal() == IDOK)
	{
		m_sou = ma.GetPathName();
	}
	//m_sleep = 1000;
	//m_lcount = 3;
	UpdateData(FALSE);
}

void CMFCConDlg::OnBnClickeddes()
{
	// TODO: Add your control notification handler code here
	CFileDialog ma(FALSE);


	if(ma.DoModal() == IDOK)
	{
		m_des = ma.GetPathName();
	}
	//m_sleep = 1000;
	//m_lcount = 2;
	UpdateData(FALSE);
}




void CMFCConDlg::OnBnClickedrun()
{
	// TODO: Add your control notification handler code here
	long gh;
	int num = 0;
	int offset = 0;
	UpdateData(TRUE);
	num = TinhByte(m_chb1,m_chb2,m_chb3,m_chb4,m_chb5);
	num = num+TinhByte(m_chb6,m_chb7,m_chb8,m_chb9,m_chb10)-1;
	if(num<=0)
		return;
	FILE *fr;
	char* cstr1 = ConvertToChar(m_sou);
	char* cstr2 = ConvertToChar(m_des);
	char *newstr = (char*)malloc(num*sizeof(char));
	fr = fopen(cstr1,"r");
	char buf[128];
	FILE *fw;
	fw = fopen(cstr2,"wt+");
	
	int count = 0;
	char temp;
	bool flag = true;
	bool fsleep = false;
	int questnum = 7;
	while(!feof(fr))
	{
			count = -1;
			do
			{
				flag=true;
				fread(&temp,1,1,fr);
				if(temp == '-' || temp == '\n' || temp == 'Q')
				{
					flag = false;
					//break;
				}
				count = count  + 1;
				buf[count] = temp;
				
	
			}while(flag == true);
			if(count < 46)
			{
				if(fsleep == false)
				{
					fprintf(fw,"--Q%03d\n",questnum++);
					fprintf(fw,"sleep(%d);\n",m_questsleep);
					
					fsleep = true;

				}
				continue;
			}
			
			setcon(newstr,num,0,' ');
			if(m_chb10==TRUE)
				setdata(newstr,buf,m_txtb10,108,TRUE,offset);
			if(m_chb9==TRUE)
				setdata(newstr,buf,m_txtb9,96,TRUE,offset);
			if(m_chb8==TRUE)
				setdata(newstr,buf,m_txtb8,84,TRUE,offset);
			if(m_chb7==TRUE)
				setdata(newstr,buf,m_txtb7,72,TRUE,offset);
			if(m_chb6==TRUE)
				setdata(newstr,buf,m_txtb6,60,TRUE,offset);
			if(m_chb5==TRUE)
				setdata(newstr,buf,m_txtb5,48,TRUE,offset);
			if(m_chb4==TRUE)
				setdata(newstr,buf,m_txtb4,36,TRUE,offset);
			if(m_chb3==TRUE)
				setdata(newstr,buf,m_txtb3,24,TRUE,offset);
			if(m_chb2==TRUE)
				setdata(newstr,buf,m_txtb2,12,TRUE,offset);
			if(m_chb1==TRUE)
				setdata(newstr,buf,m_txtb1,0,TRUE,offset);


			
			fprintf(fw,"for k = 1, %d do\n",m_lcount);
			if(m_lcount <=1)
			{
				fprintf(fw,"\tsleep(%d);\n",m_sleep);
			}
			
			
			fputs("\tsl = AOBScan(\"",fw); //sl = AOBScan("3d 01 00 00 02 00 00 00 01 00 00 00 ?? ?? ?? ?? 02 00 00 00")
			fwrite(newstr,1,num,fw);
			fputs("\")\n",fw);

			fputs("\tif(sl == nil) then\n",fw); //if(sl == nil) then
			
			fputs("\t\tif(k == 1) then \n",fw);
			fprintf(fw,"\t\t\tsleep(%d); end\n",m_ssleep);
			
			fputs("\telse\n",fw); //else
			fputs("\t\tj = stringlist_getCount(sl);\n",fw); //j = stringlist_getCount(sl);
			fputs("\t\tfor i = 1, j do\n",fw);	//for i = 1, j do
			
			fputs("\t\t\taddress=(\"0x\"..stringlist_getString(sl,i-1));\n",fw); //address=("0x"..stringlist_getString(sl,i-1));
			
			fprintf(fw,"\t\t\taddress = address + 0x%02X;\n",offset);
			
			fputs("\t\t\twriteBytes(address,0x03);\n",fw); //writeBytes(address,0x03);
			fputs("\t\tend\n",fw);
			fputs("\tend\n",fw);
			fputs("end\n",fw);
			fsleep = false;
	
	}
	fputs("object_destroy(sl);",fw);
	fclose(fr);
	fclose(fw);
	free(cstr1);
	free(cstr2);
	//ghi file config.ini
	FILE *f;
	f= fopen(ConvertToChar(apppath),"w+");
	fprintf(f,"%d\n",m_questsleep);
	fprintf(f,"%d\n",m_sleep);
	fprintf(f,"%d\n",m_lcount);
	fprintf(f,"%d\n",m_ssleep);
	if(m_chb1==TRUE)
	{
		fputs(ConvertToChar(m_txtb1),f);
		fputs("\n",f);
	}
	if(m_chb2==TRUE)
	{
		fputs(ConvertToChar(m_txtb2),f);
		fputs("\n",f);
	}
	if(m_chb3==TRUE)
	{
		fputs(ConvertToChar(m_txtb3),f);
		fputs("\n",f);
	}
	if(m_chb4==TRUE)
	{
		fputs(ConvertToChar(m_txtb4),f);
		fputs("\n",f);
	}
	if(m_chb5==TRUE)
	{
		fputs(ConvertToChar(m_txtb5),f);
		fputs("\n",f);
	}
	if(m_chb6==TRUE)
	{
		fputs(ConvertToChar(m_txtb6),f);
		fputs("\n",f);
	}
	if(m_chb7==TRUE)
	{
		fputs(ConvertToChar(m_txtb7),f);
		fputs("\n",f);
	}
	if(m_chb8==TRUE)
	{
		fputs(ConvertToChar(m_txtb8),f);
		fputs("\n",f);
	}
	if(m_chb9==TRUE)
	{
		fputs(ConvertToChar(m_txtb9),f);
		fputs("\n",f);
	}
	if(m_chb10==TRUE)
	{
		fputs(ConvertToChar(m_txtb10),f);
		fputs("\n",f);
	}
	fclose(f);
}

void CMFCConDlg::OnEnChangelcount()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
	
	
}
